// 博文 C++输入文件流遇空白符结束
// ifs 真值
#include <iostream>
#include <fstream>
#include <string.h>
int main(void)
{
  std::fstream fs;
  char str[100];

  fs.open("E:\\CodeField\\Code_Cpp\\C_Single\\csdn\\20211010\\src\\afile.dat", std::ios::trunc | std::ios::out);

  std::cout << "enter sentence 1 : ";
  std::cin >> str;
  fs << str;

  std::cout << "enter sentence 2 : ";
  std::cin >> str;

  fs << str << '\t';

  std::cout << "enter sentence 3 : ";
  std::cin >> str;
  fs << str;

  fs.close();

  // 读取文件a,并复制到b
  std::ifstream ifs;
  ifs.open("E:\\CodeField\\Code_Cpp\\C_Single\\csdn\\20211010\\src\\afile.dat");

  // ofs.is_open();

  std::ofstream ofs;
  ofs.open("E:\\CodeField\\Code_Cpp\\C_Single\\csdn\\20211010\\src\\bfile.dat", std::ios::trunc);

  int i = 0;
  char ch = '\0';
  ifs.unsetf(std::ios::skipws); // 取消默认的忽视空白字符
  // bool x = (ifs >> str); // 不存在从 "std::basic_istream<char, std::char_traits<char>>" 到 "bool" 的适当转换函数
  while (ifs)
  {
    std::cout << "get from ifs" << std::endl;
    ch = ifs.get(); // 该函数会改变文件位置指针？？
    if (ch == EOF)
    {
      std::cout << "read file is over!" << std::endl;
      // break;
    }
    else if (ch == '\n' || ch == '\t' || ch == '\v' || ch == '\f' || ch == 'r' || ch == ' ')
    {
      std::cout << "it is a white-space character !!!" << std::endl;

      std::cout << "chunk" << ++i << " : " << ch << std::endl;
      ofs << ch; // 保存空白字符
    }
    else
    {
      ifs.seekg(-1, std::ios::cur); // 普通字符回位,中文字符这边会有问题
      std::cout << "ch : " << ch << std::endl;

      strcpy(str,"");
      ifs >> str; // 这个放里面！！！
      std::cout << "chunk" << ++i << " : " << str << std::endl;
      ofs << str;
    }
  }

  ifs.close();
  ofs.close();

  printf("--------------end--------------\n");
  return 0;
}
